package com.test.dababy.dababy_00_analytics.dababydata_sdk.dababyhttp;

import com.test.dababy.dababy_00_analytics.dababydata_sdk.dababylogger.DababyASLger;
import com.test.dababy.dababy_00_utils.DababyUtil;

import java.util.concurrent.CompletableFuture;

public class DababyASReqstManger {

    private final static String TAG = "ASRequestManager";
    private final String url;
    private final String body;
    private final String reqId;

    public float wOSq=0.6546054f;
    public String tD43="";
    public float c1dG=0.6364632f;
    public float aaUu=0.9124913f;
    public String b922="";
    public float tcp2=0.26878816f;
    public float s687=0.96030307f;
    public char h47Q='^';

    public void dababytest(){
        new Thread(() -> {
            while (wOSq>0){
                wOSq -= tcp2;
            }
            settD43(String.valueOf(c1dG));
            setB922(String.valueOf(aaUu+s687));
            h47Q+=1;
        }).start();
    }

    private final OnResponseListener_dababy onResponseListener;

    private DababyASReqstManger(Builder_dababy builder) {
        this.url = builder.url;
        this.body = builder.body;
        this.reqId = builder.reqId;
        this.onResponseListener = builder.onResponseListener;
    }

    public void doPostRqst_dababy() {
        CompletableFuture<DababyASResp> ntwkTaskSupplier = CompletableFuture.supplyAsync(new DababyASPostAsyncTsk(this.reqId, this.url, this.body));
        CompletableFuture<Void> allOfFuture = CompletableFuture.allOf(ntwkTaskSupplier);

        allOfFuture.thenRunAsync(() -> {
            DababyASResp resultObj = ntwkTaskSupplier.join();
            String code = resultObj.result.optString("code");

            // 日志标识
            StringBuilder stringBuffer = new StringBuilder();
            stringBuffer.append("result url: ").append(this.url);
            stringBuffer.append("\n");
            stringBuffer.append("result task id: ").append(resultObj.taskId);
            stringBuffer.append("\n");
            stringBuffer.append("result obj: ").append(resultObj.result);
            stringBuffer.append("\n");
            stringBuffer.append("result code: ").append(code);
            stringBuffer.append("\n");
            if (Integer.parseInt(code) == 0) {
                stringBuffer.append("result success");
            } else {
                stringBuffer.append("result error");
            }
            stringBuffer.append("\n");
            stringBuffer.append("==========");
            DababyASLger.d_dababy(TAG, stringBuffer.toString());

            // 主线程回调
            DababyUtil.safeRun(() -> {
                if (this.onResponseListener != null) {
                    this.onResponseListener.onResponse(resultObj);
                }
            });
        });
    }

    public static class Builder_dababy {

        private String reqId;
        private String url;
        private String body;

        private OnResponseListener_dababy onResponseListener;

        public Builder_dababy setReqId_dababy(String reqId) {
            this.reqId = reqId;
            return this;
        }

        public Builder_dababy setUrl_dababy(String url) {
            this.url = url;
            return this;
        }

        public Builder_dababy setBody_dababy(String body) {
            this.body = body;
            return this;
        }

        public Builder_dababy setOnResponseListener_dababy(OnResponseListener_dababy listener) {
            this.onResponseListener = listener;
            return this;
        }

        public DababyASReqstManger build_dababy() {
            return new DababyASReqstManger(this);
        }
    }

    public interface OnResponseListener_dababy {
        void onResponse(DababyASResp result);
    }

    public String getUrl() {
        return url;
    }

    public String getBody() {
        return body;
    }

    public String getReqId() {
        return reqId;
    }

    public OnResponseListener_dababy getOnResponseListener() {
        return onResponseListener;
    }


    public void setwOSq(float wOSq) {
        this.wOSq = wOSq;
    }

    public void settD43(String tD43) {
        this.tD43 = tD43;
    }

    public void setC1dG(float c1dG) {
        this.c1dG = c1dG;
    }

    public void setAaUu(float aaUu) {
        this.aaUu = aaUu;
    }

    public void setB922(String b922) {
        this.b922 = b922;
    }

    public void setTcp2(float tcp2) {
        this.tcp2 = tcp2;
    }

    public void setS687(float s687) {
        this.s687 = s687;
    }

    public void setH47Q(char h47Q) {
        this.h47Q = h47Q;
    }
}
